<template>
  <div class="template-box" @click.stop="true">
    <div class="template-center box-mg-fter-height">
      <div class="top-boxs">
        <span class="top-boxs-name top-boxs-name-style">{{ info.name }}</span>
        <div class="top-boxs-bottom">
          <span class="template-heard-left-btm-text1">{{ info.mode === 1 ? '模版模式' : '自由模式' }} </span>
          <span class="template-heard-left-btm-text2">{{ info.typeValue }}</span>
          <span class="template-heard-left-btm-text2" style="margin-left: 8px">素材{{ info.code }}</span>
        </div>
      </div>
      <div class="template-center-left box-mg-fter-height-2">
        <lookComponent
          @emitCurrent="emitCurrent"
          v-if="info.id"
          :info="info"
          :colorIndex="colorIndex"
          @indexCurrent="indexCurrent"
        />
        <div class="flot-menu" id="templateInfo-name-409">
          <t-popup :visible="select1Show" attach="#templateInfo-name-409" v-if="info.colors && info.colors.length > 1">
            <template #content>
              <div class="color-list-box" v-if="info.colors">
                <span class="color-item-title">配色</span>
                <div class="color-item-list">
                  <div
                    class="list-item"
                    :class="{ 'list-item-active': index === colorIndex }"
                    @click="colorIndexChage(index)"
                    :style="{ marginRight: index === 2 ? '0px' : '' }"
                    v-for="(item, index) in info.colors"
                    :key="item.id"
                  >
                    <img class="active-box-ft" :src="publicUrls + 'door/colorActive.png'" alt="" />
                    <img class="item-imgs" :src="urls + item.coverUrl" alt="" />
                    <span>{{ item.name }}</span>
                  </div>
                </div>
              </div>
            </template>
            <div class="heard-select" @click="setShow(1)">
              <i class="doorIcon"> &#xe641; </i>
            </div>
          </t-popup>
          <a target="_blank" :href="info.previewUrl" class="right-box2" v-if="info.previewUrl">
            <span style="margin-left: 1px">预览模板</span>
          </a>
          <t-tooltip content="展览已发布无法更换模板" v-if="info.useType === 1">
            <div class="right-box1-lock">
              <!-- 展览已发布无法更换模板 -->
              <span style="margin-left: 1px">立刻使用</span>
            </div>
          </t-tooltip>
          <t-tooltip content="展览审核无法更换模板" v-if="info.useType === 4">
            <div class="right-box1-lock">
              <!-- 展览已发布无法更换模板 -->
              <span style="margin-left: 1px">立刻使用</span>
            </div>
          </t-tooltip>
          <div class="right-box1" @click="make" v-if="info.useType === 0">
            <span style="margin-left: 1px">立刻使用</span>
          </div>
          <!-- <div class="right-box1" @click="make" v-if="info.isUse">
            <span style="margin-left: 1px">立刻使用</span>
          </div> -->
        </div>
      </div>
    </div>
    <div class="template-content" v-if="info">
      <!-- <div class="right-success">
        <t-icon name="info-circle" size="22" style="color: rgba(0, 0, 0, 0.38); margin-top: 0px"></t-icon>
        <span>模版素材来自 {{ info.source }}，如有侵权，请联系平台删除</span>
      </div> -->
      <div class="detail-box">
        <div class="scroll-item1" style="margin-top: 8px">
          <span class="scroll-item-title">简介</span>
          <span class="scroll-item-description">{{ info.description }}</span>
        </div>
        <div class="scroll-item" style="margin-top: 8px">
          <span class="scroll-item-title">标签</span>
          <div class="tag-item">
            <span class="tag-item-span" v-for="item in info.labels" :key="item">{{ item.name }}</span>
          </div>
        </div>
      </div>
      <projackList :templateId="Number(templateId)" v-if="templateId" />
      <div class="template-bottom" v-if="list.length > 0"> 
        <span class="template-bottom-title">相关素材</span>
        <div class="templateList-box">
          <t-row :gutter="24">
            <t-col :span="3" v-for="(item, index) in list" :key="item.id">
              <div class="templateList-box-item" @click="addTEmplate(item.id, item.mode)">
                <div class="box-item-img-box">
                  <img
                    :src="urls + item.coverUrl + '?imageView2/2/w/624/format/webp'"
                    class="box-item-img t-image--fit-cover t-image--position-center"
                    alt=""
                  />
                </div>
                <div class="templateList-box-item-btm">
                  <span class="templateList-name">{{ item.name }}</span>
                  <div class="templateList-box-item-btm-right">
                    <img style="width: 16px; height: 16px" :src="publicUrls + 'assets/img/clickIcon2.png'" alt="" />
                    <span>{{
                      item.exhibitTotal > 999 ? '999+' : item.exhibitTotal !== 0 ? item.exhibitTotal : ''
                    }}</span>
                  </div>
                </div>
              </div>
            </t-col>
          </t-row>
        </div>
      </div>
    </div>

    <div class="ModeSelect-parent">
      <t-dialog placement="center" :visible="ModeSelectShow" @close="ModeSelectShow = false">
        <!-- :on-close="ModeSelectShow = false" -->
        <template #body>
          <div class="ModeSelect-box">
            <div class="ModeSelect-top">
              <div class="top-left"> 
                <i class="doorIcon">&#xe668;</i>
                <span>确定选择{{ info.mode === 1 ? '模版' : '自由' }}模式吗</span>
              </div>
              <i class="doorIcon" @click="ModeSelectShow = false">&#xe63d;</i>
            </div> 
            <p class="ModeSelect-center">若选择此模式则不可再变更为{{ info.mode !== 1 ? '模版' : '自由' }}模式</p>
            <div class="ModeSelect-bottom">
              <t-button theme="default" variant="outline" @click="ModeSelectShow = false">取消</t-button>
              <t-button @click="ModeSelectSubmit">确认选择</t-button>
            </div>
          </div>
        </template>
      </t-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
// @ts-nocheck  
 
import { publicUrls, cancatUrl } from '@/utils/minxin'; 
import { getHeardToken } from '@/api/utils';
import { linkurl } from '@/utils/minxin';
import projackList from './components/projackList.vue';
import { list3 } from './index';
import { projectLists, templateProject, flutTemplateList } from '@/api/project/project';
import { relateTemplateList, setTemplate, templateInfo, configData } from '@/api/templates/templates';
import { urls } from '@/utils/request/upload';
import lookComponent from './components/lookComponent.vue';
import textMax from './components/textMax.vue';
import { ref, reactive, nextTick } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useProjectStore } from '@/store';
import { MessagePlugin } from 'tdesign-vue-next';

const props = defineProps({
  id: {
    type: Number,
  },
  initMode: {
    type: Number,
  },
});
const emit = defineEmits(['emitStyleInfo', 'addTEmplateEmit']);
const store = useProjectStore();
const route = useRoute();
const info = ref({});
const modeType = ref(0); //当前选择的模版或者场景类型
const ModeSelectShow = ref(false); //确认模式弹框
const visible = ref(false);
const templateId = ref(0);
const selectList = ref([]);
const select1Show = ref(false);
const colorIndex = ref(0);
const projectId = ref(0);
const parmas = reactive({
  pageSize: 4,
  id: 0,
});
const remask = '';
const colorList5: Array<any> = [
  {
    img: publicUrls + 'template/template5/colorImg/style1.png',
    id: 1,
    name: '乌金落霞',
  },
  {
    img: publicUrls + 'template/template5/colorImg/style2.png',
    id: 2,
    name: '地平野阔',
  },
  {
    img: publicUrls + 'template/template5/colorImg/style3.png',
    id: 3,
    name: '平沙落雁',
  },
];

const getUrlTemplateId = () => {
  const url = window.location.href;
  const regex = /\/(\d+)/; // 匹配数字参数的正则表达式
  const match = url.match(regex); // 使用正则表达式匹配数字参数

  if (match) {
    const numericParam = parseInt(match[1]); // 提取数字参数并将其转换为数字类型
    parmas.id = Number(props.id);
    templateId.value = Number(props.id);
    projectId.value = Number(numericParam);
  }
};
getUrlTemplateId();
const make = () => {
  if (props.initMode === 0) {
    ModeSelectShow.value = true;
    return;
  }
  let token = getHeardToken(); 
  if (!token) {
    MessagePlugin.error('请登录再进行操作');
    return;
  }
  if (parmas.id) {
    onConfirmAnother();
    return;
  }
  // selectInit(1);

  // visible.value = true;
};
const selectInit = async (type: number) => {
  try {
    let res = await flutTemplateList(props.id);
    selectList.value = res.data;
    visible.value = true;
  } catch (err) {
    console.log(err);
  }
};
const list = ref([]);
const emitCurrent = () => {
  select1Show.value = false;
};
const init = async () => {
  try {
    let res = await templateInfo(templateId.value, projectId.value);
    res.data.colors.forEach((element) => {
      element.isShow = true; 
    });
    info.value = res.data;
    modeType.value = res.data.mode; 
    colorIndex.value = 0;
    // window.document.title = `记忆网`;
    // window.document.title = `记忆网-${info.value.name}`;
    let { data } = await relateTemplateList(parmas);
    list.value = data;
    // list.value=list.value.concat(listcancat)
  } catch {}
};
const indexCurrent = (num: number) => {
  info.value.colors[num].isShow = !info.value.colors[num].isShow;
};
const addTEmplate = (id: number, mode: number) => {
  templateId.value = id;
  init();
  nextTick(() => {
    colorIndex.value = 0;
  }); 
  modeType.value = mode; 
  // addTEmplate
  // window.scrollTo(0, 0);
};
  
init(); 
const setShow = (type: number) => {
  select1Show.value = !select1Show.value;
};
const close = (context) => {};
const onConfirmAnother = (context?: any) => {
  if (projectId.value) {
    setTemplate(projectId.value, templateId.value).then((res) => {
      ModeSelectShow.value = false;
      if (res.statusCode == 200) {
        configData(projectId.value).then((res) => {
          if (res.statusCode == 200 && res.succeeded) {
            emit('emitStyleInfo', projectId.value, modeType.value);
            // let url = `http://workbench.wuzhan.art/#/${urlCancat}?projectId=` + projectId.value;
            // window.open(url, '_blank');
          }
          visible.value = false;
        });
      } else {
      }
    });
  } else {
    MessagePlugin.error('请选择展览');
  }
  // console.log('点击了确认按钮', context);
  // visible.value = false;
};
const colorIndexChage = (index: number) => {
  colorIndex.value = index;
};
const ModeSelectSubmit = () => {
  ModeSelectShow.value = false;
  onConfirmAnother();
};
</script>

<style lang="less" scoped>
.template-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  min-height: calc(100vh - 300px);
  padding-bottom: 50px;

  border-radius: 6px;
}
.template-content {
  padding: 0 40px;
  min-width: 1152px;
  width: 100%;
}
.template-heard {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-bottom: 50px;
}

.template-heard-left {
  width: 100%;
  display: flex;
  flex-direction: column;
  margin-bottom: 24px;
}
.template-heard-title {
  font-size: 22px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #fff;
  line-height: 34px;
  margin-top: 8px;
}
.template-heard-left-btm {
  display: flex;
  align-items: center;
}
.top-boxs {
  min-width: 1152px;
  width: 80%;
  display: flex;
  flex-direction: column;
  margin: 0 auto;
}
.top-boxs-name {
  margin-top: 28px;
  padding: 0 40px;
}
.top-boxs-name-style {
  font-size: 24px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.8);
  line-height: 36px;
}
.top-boxs-bottom {
  display: flex;
  margin-top: 8px;
  padding: 0 40px;
  margin-bottom: 28px;
}
.template-heard-left-btm-text1 {
  font-size: 16px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.59);
  line-height: 24px;
  margin-right: 20px;
}
.template-heard-left-btm-text2 {
  display: flex;
  padding: 1px 8px;
  border-radius: 4px;
  background: #f0f0f0;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;

  color: rgba(0, 0, 0, 0.8);
  line-height: 22px;
  margin-right: 0px;
}
.template-heard-right {
  display: flex;
}
.right-box {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 84px;
  height: 32px;
  border-radius: 4px;
  border: 1px solid #d2ad77;
  span {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #d2ad77;
    line-height: 20px;
    margin-left: 4px;
  }
}
.scroll-box-heard-box {
  padding-bottom: 24px;
  box-shadow: inset 0px -1px 0px 0px rgba(255, 255, 255, 0.08);
  margin-bottom: 24px;
}
.flot-menu {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  bottom: 20px;
  left: 0;
  width: 100%;
  z-index: 120;
  .heard-select {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 18px;
    border: 1px solid #ffffff;
    margin-right: 12px;
    .doorIcon {
      font-size: 24px;
      color: #fff;
    }
  }
  .heard-select:hover {
    background: #434343;
  }
  .right-box1 {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 228px;
    height: 36px;
    background: #e62b17;
    border-radius: 18px;
    cursor: pointer;
    padding: 6px 16px;
    margin-left: 12px;
    img {
      width: 24px;
      height: 24px;
    }
    span {
      color: #fff;
      height: 22px;

      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #ffffff;
      line-height: 22px;
    }
  }
  .right-box1-lock {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 228px;
    height: 36px;
    background: #e62b17;
    border-radius: 18px;
    padding: 6px 16px;
    margin-left: 12px;
    opacity: 0.5;
    img {
      width: 24px;
      height: 24px;
    }
    span {
      color: #fff;
      height: 22px;

      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #ffffff;
      line-height: 22px;
    }
  }
  .right-box1-num {
    margin: 0 12px;
    width: 1px;
    opacity: 0.2;
    height: 12px;
    background: #fff;
  }
  .right-box2 {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 144px;
    height: 36px;
    border-radius: 18px;
    border: 1px solid #ffffff;
    img {
      width: 24px;
      height: 24px;
    }
    span {
      font-size: 16px;
      font-weight: 500;

      color: #ffffff;
      line-height: 24px;
    }
  }
  .right-box1:hover {
    background: #f2573f;
  }
  .right-box2:hover {
    background: #434343;
  }
}

.template-center {
  display: flex;
  flex-direction: column;
  width: 100%;
}
.template-center-left {
  position: relative;
  // width: calc(312px + 696px);

  background: rgba(0, 0, 0, 0.8);
  // border-radius: 8px 0px 0px 8px;
}
.template-center-right {
  display: flex;
  flex-direction: column;
  width: 328px;

  padding: 24px;

  background: rgba(0, 0, 0, 0.8);

  // border-radius: 0px 8px 8px 0px;
}
.scroll-box {
  width: 100%;
  flex: 1;

  overflow: auto;
}
.scroll-box::-webkit-scrollbar {
  display: none;
}
.right-success {
  display: flex;
  align-items: center;
  height: 22px;
  margin-top: 4px;
  span {
    margin-left: 4px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.38);
    line-height: 22px;
  }
}
.scroll-item {
  display: flex;
  flex-direction: column;
}
.scroll-item-title {
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 600;
  color: #ffffff;
  line-height: 24px;
  margin-bottom: 12px;
}
.scroll-item-list {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.list-item {
  width: 104px;
  height: 94px;
  display: flex;
  flex-direction: column;
  padding: 8px;
  img {
    width: 88px;
    height: 56px;
    border-radius: 5px;
    margin-bottom: 8px;
  }
  span {
    text-align: center;
    width: 100%;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;

    color: #ffffff;
    line-height: 22px;
    margin-bottom: 12px;
  }
  .active-box-ft {
    position: absolute;
    bottom: -3px;
    right: -3px;
    width: 32px;
    height: 32px;
    opacity: 0;
  }
}
.list-item:hover {
  border: 1px solid #fff;
}
.tag-item {
  display: flex;
  flex-wrap: wrap;
  .tag-item-span {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1px 8px;
    // height: 22px;
    border-radius: 4px;

    border: 1px solid #cfbda8;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(207, 189, 168, 1);
    line-height: 22px;
    margin-bottom: 12px;
    margin-right: 12px;
  }
}
.template-bottom {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin-top: 32px;
}
.template-bottom-title {
  font-size: 22px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 600;
  color: #070707;
  line-height: 34px;
  margin-bottom: 24px;
}

.templateList-box {
  width: 100%;
  // display: flex;
  // justify-content: space-between;
}
.t-col {
  margin-bottom: 0px;
}
.t-row {
  margin-top: 0;
}
.templateList-box-item {
  cursor: pointer;
  display: flex;
  flex-direction: column;
  width: 100%;

  .box-item-img-box {
    position: relative;
    width: 100%;
    padding-top: 66.6%;
    .box-item-img {
      position: absolute;
      top: 0;
      right: 0;
      transition: all 0.3s;
      min-width: 100%;
      max-width: 100%;
      min-height: 100%;
      max-height: 100%;
      object-fit: contain;
      margin: auto;
      display: block;
      width: 0px;
      height: 0px;
      background: #eeeeee;
      border-radius: 5px;

      // border: 1px solid rgba(207, 189, 168, 0.3);
    }
  }
}
.templateList-box-item-btm {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-top: 16px;
  padding: 0 4px;
}
.templateList-name {
  font-size: 16px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #333333;
  line-height: 24px;
}
.templateList-box-item-btm-right {
  display: flex;
  align-items: center;
  img {
    width: 16px;
    height: 16px;
    border: none;
  }
  span {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #999999;
    line-height: 22px;
    margin-left: 3px;
  }
}
.select {
  display: flex;
  align-items: center;
  width: 100%;
}
:deep(.t-dialog) {
  width: 520px;
  height: 208px;
}
.template-boxs {
  width: 100px;
}
.select-right {
  flex: 1;
}
.templateList-box-item:hover {
  .box-item-img-box {
    .box-item-img {
      position: absolute;
      top: -8px;
      background: #eeeeee;
      border-radius: 8px;
      box-shadow: 0px 4px 10px 0px rgba(217, 217, 217, 0.4);
    }
  }

  .remask-box {
    display: flex;
  }
}

.box-mg-fter-height {
  height: calc(528px + 124px);
  margin-bottom: 32px;
}
.box-mg-fter-height-2 {
  height: calc(528px + 124px);
}
.detail-box {
  display: flex;
  width: 100%;
  // margin-top: 32px;
  .scroll-item1 {
    display: flex;
    flex-direction: column;
    width: 64%;
    margin-right: 50px;
  }

  .scroll-item {
    display: flex;
    flex-direction: column;
    flex: 1;
  }
  .scroll-item-title {
    font-size: 18px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    color: #070707;
    line-height: 28px;
  }
  .scroll-item-description {
    font-size: 16px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.8);
    line-height: 32px;
  }

  .tag-item {
    .tag-item-span {
      border: 1px solid #e62b17;
      color: #e62b17;

      border-radius: 4px;
    }
  }
}

.color-list-box {
  display: flex;
  flex-direction: column;
  width: 100%;
  width: 368px;
  height: 264px;
  padding: 12px 16px;
}
.color-item-title {
  font-size: 16px !important;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff !important;
  line-height: 24px;
}
.color-item-list {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  border-radius: 3px;
}
.list-item {
  position: relative;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 104px;
  height: 94px;
  margin-right: 12px;
  margin-top: 12px;
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.15);
  background: #434343;
  border: 1px solid transparent;
  .item-imgs {
    width: 88px;
    height: 56px;
    margin-bottom: 4px;
  }
  span {
    text-align: center;
    width: 100%;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #ffffff;
    line-height: 22px;
  }
}

.list-item-active {
  border: 1px solid #f0f0f0;
  .active-box-ft {
    opacity: 1;
    margin: 0;
  }
}

:deep(.t-popup) {
  border-radius: 8px !important;
  background: transparent;
}
:deep(.t-popup[data-popper-placement^='bottom'] .t-popup__content) {
  border-radius: 8px !important;
}
:deep(.t-popup__content) {
  background: rgba(38, 38, 38, 0.99);
  border-radius: 8px !important;
  overflow: hidden;
  backdrop-filter: blur(12px);
  padding: 0;
  border: none;
  box-shadow: none;
}
.ModeSelect-parent {
  :deep(.t-dialog__header) {
    display: none;
  }
  :deep(.t-dialog__footer) {
    display: none;
  }
  :deep(.t-dialog__body) {
    padding: 0px;
    scrollbar-width: none;
    width: 528px;
  }
  :deep(.t-dialog__body::-webkit-scrollbar) {
    display: none;
  }
  :deep(.t-dialog) {
    min-width: 528px;
    height: 190px;
    scrollbar-width: none;
  }
}
.ModeSelect-box {
  display: flex;
  flex-direction: column;
  width: 528px;
  height: 190px;
  padding: 20px 24px;
  scrollbar-width: none;
}

.ModeSelect-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  .doorIcon {
    cursor: pointer;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.38);
  }
  .top-left {
    display: flex;
    align-items: center;
    font-family: PingFang SC;
    font-size: 18px;
    font-weight: 400;
    line-height: 28px;
    text-align: left;

    .doorIcon {
      font-size: 24px;
      color: rgba(227, 115, 24, 1);
      margin-right: 8px;
    }
  }
}

.ModeSelect-center {
  font-family: PingFang SC;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  text-align: left;
  color: rgba(0, 0, 0, 0.59);
  margin-top: 20px;
  margin-bottom: 40px;
}
.ModeSelect-bottom {
  display: flex;
  justify-content: flex-end;
  :deep(.t-button) {
    height: 36px;
    margin-left: 12px;
  }
}
</style>
